package org.simp.node;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.simp.common.Constants;
import org.simp.context.SimpContext;
import org.simp.core.message.SimpMessage;
import org.simp.handler.AbstractSimpHandler;
import org.simp.interceptor.SimpHandlerInterceptor;
import org.simp.router.RouterInterceptor;

/**
 * @author zhuhaitao at 2014-5-26
 *
 *Simp 通信节点 
 */
public class SimpNode {
	
	private  SimpClientConnector clientConnector;
	
	private SimpServerAccepter accepter;
	
	private String domain;
	
	private String nodeName;
	
	private int nodeId;
	
	private String nodeType;
	
	public SimpNode(int nodeId,String nodeName,String nodeType){
		this.nodeId = nodeId;
		this.nodeName = nodeName;
		this.nodeType = nodeType;
	}
	public void initServer(String ip,int port,SimpContext context,AbstractSimpHandler simpHanlder,RouterInterceptor routerInterceptor ,SimpHandlerInterceptor handlerInterceptor,ProtocolCodecFilter[] codecFilters){
		accepter = new SimpServerAccepter(ip, port, nodeName, context, simpHanlder,this);
		accepter.setCodecFilters(codecFilters);
		accepter.setHandlerInterceptor(handlerInterceptor);
		accepter.setRouterInterceptor(routerInterceptor);
	}
	
	public void initClient(String ip,int port,String serverName,SimpContext context,AbstractSimpHandler simpHanlder,RouterInterceptor routerInterceptor ,SimpHandlerInterceptor handlerInterceptor,ProtocolCodecFilter[] codecFilters){
		clientConnector = new SimpClientConnector(ip, port,serverName, nodeName, context, simpHanlder,this);
		clientConnector.setAutoReConnecting(true);
		clientConnector.setHandlerInterceptor(handlerInterceptor);
		clientConnector.setRouterInterceptor(routerInterceptor);
		clientConnector.setCodecFilters(codecFilters);
	}
	
	public void initClient(String ip,int port,String serverName,AbstractSimpHandler simpHanlder,SimpHandlerInterceptor handlerInterceptor){
		clientConnector = new SimpClientConnector(ip, port,serverName, nodeName, null, simpHanlder,this);
		clientConnector.setAutoReConnecting(true);
		clientConnector.setHandlerInterceptor(handlerInterceptor);
	}
	
	public void initClient(String ip,int port,String serverName,String name ,SimpContext context,AbstractSimpHandler simpHanlder,RouterInterceptor routerInterceptor ,SimpHandlerInterceptor handlerInterceptor){
		clientConnector = new SimpClientConnector(ip, port,serverName, name, context, simpHanlder,this);
		clientConnector.setAutoReConnecting(true);
		clientConnector.setHandlerInterceptor(handlerInterceptor);
		clientConnector.setRouterInterceptor(routerInterceptor);
		clientConnector.setNode(this);
	}
	public void start() throws IOException{
		if(accepter != null)
			accepter.accept();
		if(clientConnector !=null)
			clientConnector.connect();
	}
	
	public void close(){
		if(clientConnector !=null)
			clientConnector.close();
		if(accepter !=null)
			accepter.close();
	}

	/**
	 * 发送给上层节点
	 * @param message
	 * @Author zhuhaitao at 2014年6月7日
	 *
	 */
	public void sendToUpNode(SimpMessage message){
		if(clientConnector !=null)
			clientConnector.send(message);
	}
	/**
	 * 发送给下层节点
	 * @param message
	 * @Author zhuhaitao at 2014年6月7日
	 *
	 */
	public void sendToDownNode(SimpMessage message ){
		if(accepter != null){
			accepter.send(message);
		}
	}

	public String getDomain() {
		return domain;
	}

	public void setDomain(String domain) {
		this.domain = domain;
	}

	public String getNodeName() {
		return nodeName;
	}

	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}
	/**
	 * 节点当前的域
	 * @return
	 * @Author zhuhaitao at 2014年6月7日
	 *
	 */
	public String getNodeDomain(){
		if(domain ==null || "".equals(domain)){
			return nodeName;
		}else{
			return nodeName + Constants.ADDRESS_SPLIT + domain;
		}
	}

	public int getNodeId() {
		// TODO Auto-generated method stub
		return nodeId;
	}

	public String getNodeType() {
		// TODO Auto-generated method stub
		return nodeType;
	}
	/**
	 * 对objAddress进行加工，
	 * @param objAddress
	 * @return
	 * @Author zhuhaitao at 2014年6月7日
	 *
	 */
	public String getObjAddress(String objAddress){
		String currentDomain = getDomain();
		String[] adresss = currentDomain.split(Constants.ADDRESS_SPLIT);
		String[] objAdresses = objAddress.split(Constants.ADDRESS_SPLIT);
		Set<String> set = new HashSet<String>();
		for(String str : objAdresses){
			set.add(str);
		}
		for(int i = adresss.length -1 ; i>=0 ;i--){
			set.add(adresss[i]);
		}
		StringBuffer sb = new StringBuffer();
		for(String str : set){
			sb.append(str).append(Constants.ADDRESS_SPLIT);
		}
		
		return sb.substring(0,sb.length() -1);
	}
	
}
